﻿using System;
using System.IO;
using System.Text;

namespace WpfAdvControls.File
{
    /// <summary>
    ///     Class for getting temp filenames and folders
    /// </summary>
    public class TempFileUtil
    {
        private string baseFolder;
        private readonly string basePrefix;

        public TempFileUtil()
        {
            basePrefix = GenerateRandomName(7);
            GenerateBaseFolder();
        }

        public TempFileUtil(string basePrefix)
        {
            this.basePrefix = basePrefix;
            GenerateBaseFolder();
        }

        public TempFileUtil(string basePrefix, string baseFolder)
        {
            this.basePrefix = basePrefix;
            this.baseFolder = baseFolder;
            if (!Directory.Exists(baseFolder))
            {
                Directory.CreateDirectory(baseFolder);
            }
        }

        public static string GenerateRandomName(int quantity)
        {
            var val = "ABCDEFGHIJKLMNOPQRSTUVWXWZabcdefghijklmnopqrstuvwxyz";
            var rand = new Random();
            var sb = new StringBuilder(quantity);
            for (var i = 0; i < quantity; i++)
            {
                var index = rand.Next(val.Length);
                sb.Append(val[index]);
            }
            return sb.ToString();
        }

        private void GenerateBaseFolder()
        {
            var tmpFolder = Path.GetTempPath();
            var folderName = basePrefix + DateTime.Now.ToString("yyyyMMddHHmmss");
            baseFolder = Path.Combine(tmpFolder, folderName);
            if (!Directory.Exists(baseFolder))
            {
                Directory.CreateDirectory(baseFolder);
            }
        }

        public string GenerateTempFileOnName(string baseFile, string filePrefix)
        {
            return null;
        }
    }
}